<?php 
/*
Template Name: them_lich Page
*/
?>
<?php 
 $id_lich=$_POST["id_lich"];
 $id_may=$_POST["id_may"];
 $id_user=$_POST["id_user"];
 $title=$_POST["title"];
 $content=$_POST["content"];
 $date_start=$_POST["date_start"];
 $date_end=$_POST["date_end"];

include_once($_SERVER['DOCUMENT_ROOT'].'/wp-load.php' );

global $wpdb;


$query="select * from wp_lich where
 `ngay_batdau` between "."'".$date_start."'"." and "."'".$date_end."'"." and id_may=".$id_may."
or `ngay_ket_thuc` between  "."'".$date_start."'"." and "."'".$date_end."'"." and id_may=".$id_may	;

$time_end=strtotime($date_end);
$time_start= strtotime($date_start);

 //echo "////////////////";
$datetime = new DateTime();
 $getdate= strtotime($datetime->format('Y-m-d H:i:s'));

// echo "////////////////";

if($getdate >= $time_start || $getdate >= $time_end ){
	echo "1";
	return;
}

$wpdb->get_results($query);
if($wpdb->num_rows!=0){
	echo "2";
	return;
}

$query="INSERT INTO wp_lich (
`id_lich` ,
`ID` ,
`id_may` ,
`ngay_batdau` ,
`ngay_ket_thuc` ,
`tieu_de_lich` ,
`noi_dung_lich` ,
`ngay_tao` ,
`ngay_sua`
)
VALUES (
$id_lich , ".$id_user.", ".$id_may.", "."'".$date_start."'".", "."'".$date_end."'".", "."'".$title."'".", "."'".$content."'".", "."'".$date_start."'".", "."'".$date_start."'"."
);";

$wpdb->query($query)or die(mysql_error());
echo "ok";
?>
